package com.bdqn.security;

import com.bdqn.mapper.SysUserMapper;
import jakarta.annotation.Resource;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Service;

import java.util.HashSet;

/**
 * UserDetailsServiceImpl 用户信息服务实现类
 *
 * @author LILIBO
 * @since 2025/4/7
 */
@Service
public class UserDetailsServiceImpl implements UserDetailsService {

    @Resource
    private PasswordEncoder passwordEncoder;

    @Resource
    private SysUserMapper sysUserMapper;

    /**
     * 根据用户名获取用户信息
     */
    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
        // 1、判断用户名是否存在，如果不存在， 抛出 UsernameNotFoundException 异常
        if (!"admin".equals(username)) {
            throw new UsernameNotFoundException("用户名不存在！");
        }
        // 2、把查询出来的密码(注册时已经加密过的)， 直接放入 User 的构造方法中
        String password = passwordEncoder.encode("123");

        // 3、返回 UserDetails 对象
        return new SecurityUser(username, password, new HashSet<>());
    }

}
